package com.thon.controller.rest;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresGuest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thon.controller.util.BaseController;
import com.thon.dao.util.Page;
import com.thon.entity.project.Region;
import com.thon.service.project.RegionService;

/**
 * 用户修改自己资料的Controller.
 * 
 * @author thon
 */
@Controller
@RequestMapping(value = "/api/region")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class RegionRestController extends BaseController {
	private static final String PAGE_SIZE = "5";

	@Autowired
	private RegionService regionService;

	//根据地区parentId查询它对应的所有子地区
	@RequestMapping(value = "/list/parent", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseEntity<HttpEntity> listRegions(
			@RequestParam(value = "parentId", defaultValue="10") int parentId,
			Model model) {

		List<Region> regions = regionService.listRegions(parentId);
		return new ResponseEntity(regions, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/list/city", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<HttpEntity> listCitys(
			@RequestParam(value = "parentId", defaultValue="10") int parentId,
			Model model) {

		List<Region> regions = regionService.listRegions(parentId);
		model.addAttribute("result", regions);
		return new ResponseEntity(model, HttpStatus.OK);
	}

	//根据地区level查询它对应的所有地区
	@RequestMapping(value = "/list/level")
	public ResponseEntity<HttpEntity> listRegionsByLevel(
			@RequestParam(value = "level") Byte level,
			@RequestParam(value="flag",defaultValue="1") int flag , Model model) {
		
		List<Region> regions = regionService.listRegionsByLevel(level);
		model.addAttribute("result", regions);
		if(flag==1){
			return new ResponseEntity(regions,HttpStatus.OK);
		}else{
			return new ResponseEntity(model,HttpStatus.OK);
		}
	}
	
	/**
	 * @param level 区域等级
	 * @param pageNo
	 * @param pageSize
	 * @param model
	 * @return
	 * 根据区域等级分页获取区域
	 */
	@RequestMapping(value = "/find/level")
	public ResponseEntity<HttpEntity> listRegionPageByLevel(
			@RequestParam(value = "level") Byte level,
			@RequestParam(value = "p",defaultValue = "1") int pageNo,
			@RequestParam(value = "s",defaultValue = "8") int pageSize,Model model) {
		
		Page<Region> page = new Page<Region>();
		page = regionService.findRegiong(pageNo, pageSize, level);
		return new ResponseEntity(page, HttpStatus.OK);
	}

	//将所有地区分页显示
	@RequestMapping(value = "/list")
	public ResponseEntity<HttpEntity> findRegionPage(
			@RequestParam(value = "p", required = false, defaultValue = "1") int p,
			@RequestParam(value = "s", required = false, defaultValue = "20") int s,
			Model model) {
		
		Page<Region> page = regionService.findRegionPage(p, s);
		return new ResponseEntity(page, HttpStatus.OK);
	}

}
